package com.isman.placeslist;

import com.isman.placeslist.apiobject.Status.Location;
import com.isman.placeslist.util.GeoLocation;
import com.isman.placeslist.util.GeoLocation.LocationResult;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class PageSearch extends Activity {
	public static final String FROM = "from";
	public static final int FROM_NOMAL = 0;
	public static final int FROM_NEWSTATUS = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_search);
		mFrom = getIntent().getIntExtra(FROM, FROM_NOMAL);

		GeoLocation geoLocation = new GeoLocation();
		geoLocation.getLocation(this, new LocationResult() {

			@Override
			public void gotLocation(final android.location.Location location) {
				if (location != null) {
					runOnUiThread(new Runnable() {

						@Override
						public void run() {
							Toast.makeText(
									getApplicationContext(),
									(location.getLatitude() * 10E6) + " "
											+ (location.getLongitude() * 10E6),
									0).show();
						}
					});
					Log.i("LOCATION", (location.getLatitude() * 10E6) + "");
					Log.i("LOCATION", (location.getLatitude() * 10E6) + "");
				} else {
					Log.i("LOCATION", "Null");
				}
			}
		});

		viBtnGo = (ImageButton) findViewById(R.id.ibtn_search_go);
		viBtnPosition = (ImageButton) findViewById(R.id.ibtn_search_position);
		vActInput = (AutoCompleteTextView) findViewById(R.id.act_search_input);
		vlvwLsit = (ListView) findViewById(R.id.lvw_search_list);

		viBtnGo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mSearch(vActInput.getText().toString());
			}
		});
		viBtnPosition.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mPosition();
			}
		});

		vlvwLsit.addFooterView(LayoutInflater.from(this).inflate(
				R.layout.inc_item_newplace, null));
		vlvwLsit.setAdapter(new Item(this));
	}

	protected void mSearch(String entry) {

	}

	protected void mPosition() {
		LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			mCreateGpsDisabledAlert();
		}
	}

	protected void mCreateGpsDisabledAlert() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Your GPS is disabled! Would you like to enable it?")
				.setCancelable(false)
				.setPositiveButton("Enable GPS",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								mShowGpsOptions();
							}
						});
		builder.setNegativeButton("Do nothing",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						finish();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}

	protected void mShowGpsOptions() {
		Intent gpsOptionsIntent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		startActivity(gpsOptionsIntent);
	}

	protected void turnGPSOn() {
		String provider = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (!provider.contains("gps")) { // if gps is disabled
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			sendBroadcast(poke);
		}
	}

	protected void turnGPSOff() {
		String provider = Settings.Secure.getString(getContentResolver(),
				Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		if (provider.contains("gps")) { // if gps is enabled
			final Intent poke = new Intent();
			poke.setClassName("com.android.settings",
					"com.android.settings.widget.SettingsAppWidgetProvider");
			poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
			poke.setData(Uri.parse("3"));
			sendBroadcast(poke);
		}
	}

	public class Item extends BaseAdapter {

		LayoutInflater inflater;

		public Item(Context mContext) {
			inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 30;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View view = inflater.inflate(R.layout.inc_item_place_item_medium,
					null);

			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Location location = new Location();
					location.setId(1000);
					location.setName("Hotel and Spa");

					switch (mFrom) {
					case FROM_NOMAL:
						Intent intent = new Intent(PageSearch.this,
								PagePlace.class);
						intent.putExtra(PagePlace.LOCATION_ID, 8);
						startActivity(intent);
						break;
					case FROM_NEWSTATUS:
						Intent intent2 = new Intent();
						intent2.putExtra(PageNewStatus.LOCATION_INFO, location);
						setResult(Activity.RESULT_OK, intent2);
						finish();
						break;
					}
				}
			});
			return view;
		}

	}

	public int mFrom = 0;

	private AutoCompleteTextView vActInput;
	private ImageButton viBtnGo;
	private ImageButton viBtnPosition;
	private ListView vlvwLsit;
}
